#include <iostream>
#include <string>
#include "complie_run.hpp"
#include "../comm/httplib.h"
using namespace complie_runNs;
using namespace utilNs;
using namespace httplib;

int main(int argc, char* args[]) {
    if (argc != 2) {
        std::cout << "Usage:\n\t" << args[0] << " local_port\n" << std::endl;
        return 0;
    }
    uint16_t port = std::stoi(args[1]);
    // Json::Value in_value;
    // Json::FastWriter writer;
    // std::string code_content;
    // FileUtil::ReadeFile(PathUtil::Src("code"), &code_content);
    // in_value["code"] = code_content;
    // std::string input_content;
    // FileUtil::ReadeFile(PathUtil::Stdin("code"), &input_content);
    // std::cout << "compile_server:: input_content == " << input_content << std::endl;
    // in_value["input"] = input_content;
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 50;
    // // 序列化
    // std::string in_json = writer.write(in_value);
    // std::string out_json;
    // std::cout << "in_json: \n" << in_json << std::endl;
    // Complie_Run::Start(in_json, &out_json);
    // std::cout << "out_json: \n" << out_json << std::endl;

    // 将编译运行服务打包成网络服务
    Server svr;

    svr.Post("/complie_and_run", [](const Request& req, Response& resp){
        std::string in_json = req.body; // req的body就是一个json串
        std::string out_json;
        if (!in_json.empty()) {
            Complie_Run::Start(in_json, &out_json);
  
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });


    // 启动svr
    svr.listen("0.0.0.0", port);
    
    return 0;
}